package com.zh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.exception.GlobalException;
import com.zh.pojo.domain.ProfitsharingRuleBase;
import com.zh.mapper.ProfitsharingRuleBaseMapper;
import com.zh.pojo.domain.ProfitsharingRuleDeductCancel;
import com.zh.pojo.domain.ProfitsharingRuleDeductComplaint;
import com.zh.pojo.result.ProfitSharingResult;
import com.zh.remote.pojo.param.DriverSummaryResult;
import com.zh.service.IProfitsharingRuleBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.service.IProfitsharingRuleDeductCancelService;
import com.zh.service.IProfitsharingRuleDeductComplaintService;
import com.zh.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 司机抽成基础比例 服务实现类
 * </p>
 *
 * @author 初四
 * @since 2024-07-06
 */
@Service
public class ProfitsharingRuleBaseServiceImpl extends ServiceImpl<ProfitsharingRuleBaseMapper, ProfitsharingRuleBase> implements IProfitsharingRuleBaseService {
    @Autowired
    private IProfitsharingRuleDeductCancelService cancelService;

    @Autowired
    private IProfitsharingRuleDeductComplaintService complaintService;

    /**
     * 计算分账
     *
     * @param realAmount
     * @param summary
     * @return
     */
    @Override
    public ProfitSharingResult DividendCalculation(BigDecimal realAmount, DriverSummaryResult summary) {
        // 1、查询分账规则
        List<ProfitsharingRuleBase> baseList = super.list();
        if (baseList.size() == 0) {
            throw new GlobalException("没有找到分账规则");
        }
        ProfitsharingRuleBase ruleBase = baseList.get(0);
        BigDecimal driverRatio = ruleBase.getDriverRatio();

        // 2、计算司机分账比例
        driverRatio = getdriverRatio(summary, driverRatio);
        // 4、计算平台抽成比例
        // 平台抽成比例 = 司机抽成比例 * 平台抽成比例
        BigDecimal platformRatio = new BigDecimal("1").subtract(driverRatio);
        // 返回分账结果
        ProfitSharingResult result = new ProfitSharingResult();
        // 司机抽成比例
        result.setDriverRate(driverRatio);
        // 平台比例
        result.setPlatformRate(platformRatio);
        // 司机分账收入
        result.setDriverIncomeAmount(realAmount.multiply(driverRatio).setScale(2, BigDecimal.ROUND_HALF_UP));
        // 平台分账收入
        result.setPlatformIncomeAmount(realAmount.multiply(platformRatio).setScale(2, BigDecimal.ROUND_HALF_UP));
        return result;
    }

    /**
     * 计算司机分账比例
     *
     * @param summary
     * @param driverRatio
     * @return
     */
    private BigDecimal getdriverRatio(DriverSummaryResult summary, BigDecimal driverRatio) {
        // 3、计算司机扣减比例
        // 3.1计算司机取消订单扣减比例    司机比例 - 取消订单扣减比例
        // 根据司机取消数量查询司机取消订单抽成扣减
        if (summary.getTodayCancel() > 0) {
            ProfitsharingRuleDeductCancel cancel = cancelService.getOne(new LambdaQueryWrapper<ProfitsharingRuleDeductCancel>()
                    .le(ProfitsharingRuleDeductCancel::getNumberFrom, summary.getTodayCancel())
                    .gt(ProfitsharingRuleDeductCancel::getNumberTo, summary.getTodayCancel()));
            AssertUtil.isNotNull(cancel, "没有找到司机取消订单抽成扣减比例");
            // 司机抽成比例 = 司机抽成比例 - 取消订单扣减比例
            driverRatio = driverRatio.subtract(cancel.getDriverRatioDeduct());

        }
        // 3、2 计算司机投诉数量扣减比例   司机比例- 投诉数量扣减比例
        // 根据司机投诉数量查询司机投诉抽成扣减
        if (summary.getTodayComplaint() > 0) {
            ProfitsharingRuleDeductComplaint complaint = complaintService.getOne(new LambdaQueryWrapper<ProfitsharingRuleDeductComplaint>()
                    .le(ProfitsharingRuleDeductComplaint::getNumberFrom, summary.getTodayComplaint())
                    .gt(ProfitsharingRuleDeductComplaint::getNumberTo, summary.getTodayComplaint()));
            AssertUtil.isNotNull(complaint, "没有找到司机投诉抽成扣减比例");
            driverRatio = driverRatio.subtract(complaint.getDriverRatioDeduct());
        }
        return driverRatio;
    }
}
